import GlobalParamsController from '../../core/controllers/GlobalParams.controller';
import storageApi from '../../core/storage/storage';

@Inject('$log', '$rootScope', '$state', '$timeout', 'globalService', 'staffService')
class PersonnelClassifyController extends GlobalParamsController {
  constructor() {
    super(globalService, $rootScope);
    this.$get();
    this.$table = {
      fetchTimes: 0,
      isTableLoading: false,
      pagination: {
        ITEMS_PER_PAGE: 10,
        MAX_SIZE: 5,
        currentPage: 1,
        total: 0,
      },
    };
    this.init();
  }

  $get() {
    this.globalService
      .queryDept({
        mapParams: {
          orgId: this.$state.params.orgId || storageApi.get('orgId'),
          typeCode: '02',
        },
      })
      .then((res) => {
        this.deptParams = res.rows;
        if (this.deptParams && this.deptParams.length > 0) {
          this.deptParams.forEach((item, index) => {
            if (index === 0) {
              this.$paginationQuery({ departmentCode: item.code });
            }
          });
        }
      });
  }

  $paginationQuery(params) {
    this.$table.fetchTimes = 0;
    this.$table.isTableLoading = true;
    params.orgId = this.$state.params.orgId || storageApi.get('orgId');
    this.staffService
      .query({
        mapParams: params,
        page: this.$table.pagination.currentPage,
        size: this.$table.pagination.ITEMS_PER_PAGE,
      })
      .then((res) => {
        this.workerListData = res.rows;
        this.$table.pagination.totalItems = res.total;
        this.$table.isTableLoading = false;
        if (res.total < 1) {
          this.$table.fetchTimes += 1;
        }
      });
  }

  $getWorkerInfoList(item, $index, type) {
    // 点击其他
    if (type) {
      this.$log.debug(item);
      this.data.otherClassify = true;
      this.data.menuOtherCurrentIndex = $index;
      $('.remove-active').siblings().removeClass('active');
      if (item.type === 'department') {
        this.param = { typeCode: item.id };
      } else if (item.type === 'officer') {
        this.param = { postCode: item.id };
      } else {
        this.param = { type: item.id };
      }
    } else {
      this.data.otherClassify = false;
      this.data.menuCurrentIndex = $index;
      this.param = { departmentCode: item.code };
    }
    const param = this.param;
    this.$paginationQuery(param);
  }

  init() {
    this.data = {
      menuCurrentIndex: 0,
      menuOtherCurrentIndex: 0,
      otherClassify: false,
      otherList: [{
        id: '2',
        name: '特种作业人员',
      }, {
        id: '3',
        name: '三类人员',
      }, {
        id: '01',
        name: '项目部人员',
        type: 'department',
      }],
    };
    // , {
    //     id: '4',
    //     name: '专职安全员传id值',
    //     type: 'officer',
    //   }
  }
}

export default PersonnelClassifyController;
